import matplotlib.pyplot as plt
import matplotlib.patches as patches


def create_rule_diagram():
    """创建规则示意图，展示附件1规则在附件2格式中的映射"""
    fig, ax = plt.subplots(figsize=(12, 8))
    ax.set_xlim(0, 12)
    ax.set_ylim(0, 8)
    ax.axis('off')

    # 标题
    ax.text(6, 7.5, 'Rule Mapping: Attachment 1 to Attachment 2 Format',
            fontsize=14, fontweight='bold', ha='center')

    # 附件2模板结构示意
    template_text = """
Standardized Prompt Template [System+User]
Protocol: {PROTO}
Path: {path_id}, len={len}
States(ordered): {S1}->...->{S_len}
AttributeCandidatesByState: {"{S1}": {KEY_1: [...], KEY_2: [...]}}
Invariants: ["state:key=<required>"]
Options: {budget: value, seed: value}
"""
    ax.text(1, 6.5, template_text, fontsize=10, fontfamily='monospace',
            bbox=dict(boxstyle="round,pad=1", facecolor='lightgray'))

    # 附件1规则示例映射
    rule_example = """
Attachment 1 Rule CE.FC01.QTY mapped:
- Expression: (fc=01) ∧ (qty≤0 ∨ qty≥2001)
- Goal: Generate counterexample frames
- Budget: 2000
- Coverage: qty values [0,1,2,1999,2000,2001]
- Constraints: Keep other guards valid
"""
    ax.text(1, 4.0, rule_example, fontsize=10, fontfamily='monospace',
            bbox=dict(boxstyle="round,pad=1", facecolor='lightblue'))

    # 连接箭头表示映射关系
    ax.annotate('', xy=(5, 5.5), xytext=(5, 6.0),
                arrowprops=dict(arrowstyle='->', lw=1.5))
    ax.text(5, 5.2, 'Mapping', ha='center', style='italic')

    # 保存图片
    plt.savefig('rule_mapping.png', dpi=300, bbox_inches='tight')
    plt.close()
    print("参考图片已生成: rule_mapping.png")


if __name__ == "__main__":
    create_rule_diagram()